// pages/addaddress/addaddress.js
const app = getApp()
var model = require('../model/model.js')
var show = false;
var item = {};
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    width:200,
    top:0,
    address:{},
    item: {
      show: show
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.Find_AddressInfo(options.id);
    this.setData({id:options.id});
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function (e) {
    var that = this;
    //请求数据
    model.updateAreaData(that, 0, e);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this;
    wx.getSystemInfo({
      success: function (res) {
        if(res.platform=="ios"){
          that.setData({width:190,top:-14});
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  Find_AddressInfo:function(id){
    var that=this;
    var data = {};
    data.act = "Find_AddressInfo";
    data.id = id;
    wx.showLoading({ title: '加载中...' });
    app.Ajax(data, function (json) {
      wx.hideLoading();
      if (json.state) {
        that.setData({ province: json.data.Province, city: json.data.City, county: json.data.Area, address:json.data});
      }
    });
  },
  formSubmit: function (e) {
    var data = {};
    data.act = "Save_Address";
    data.id=this.data.id;
    data.realname = e.detail.value.realname;
    data.mobile = e.detail.value.mobile;
    data.address = e.detail.value.address;
    data.province=this.data.province;
    data.city = this.data.city;
    data.area = this.data.county;
    if (data.realname.trim() == "") {
      app.showError("请输入真实姓名");
      return;
    }
    if (data.mobile.trim() == "") {
      app.showError("请输入手机号码");
      return;
    }
    if (data.address.trim() == "") {
      app.showError("请输入详细地址");
      return;
    }
    wx.showLoading({ title: '正在保存...' });
    app.Ajax(data, function (json) {
      wx.hideLoading();
      if (json.state) {
        app.showSuccess(json.msg);
        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2];
        prevPage.setData({refresh: true});
        setTimeout(function () {
          wx.navigateBack();
        }, 1000);
      } else {
        app.showError(json.msg);
      }

    });
  },
  //点击选择城市按钮显示picker-view
  selectCity: function (e) {
    model.animationEvents(this, 0, true, 400);
  },
  //隐藏picker-view
  hiddenFloatView: function (e) {
    model.animationEvents(this, 200, false, 400);
  },
  //滑动事件
  bindChange: function (e) {
    model.updateAreaData(this, 1, e);
    item = this.data.item;
    this.setData({
      province: item.provinces[item.value[0]].name,
      city: item.citys[item.value[1]].name,
      county: item.countys[item.value[2]].name
    });
  },
  onReachBottom: function () {
  },
  nono: function () { }
})